home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / aztecnos.arc / SESSION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-18  |  7.0 KB  |  345 lines

  1. /* Session control */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "socket.h"
  7. #include "ftpcli.h"
  8. #include "telnet.h"
  9. #include "icmp.h"
  10. #include "ax25tnc.h"
  11. #include "session.h"
  12. #include "cmdparse.h"
  13. #include "timer.h"
  14. #include "proc.h"
  15. #include "tty.h"
  16.  
  17. char *psocket(),*tcp_port();
  18. char *sockstate();
  19. void tel_upload();
  20.  
  21. struct session *Sessions;
  22. struct session *Current;
  23. struct session *Lastcurrent;
  24.  
  25. char Notval[] = "Not a valid control block\n";
  26. char Badsess[] = "Invalid session\n";
  27. char *Sestypes[] = {
  28.     "",
  29.     "Telnet",
  30.     "FTP",
  31.     "AX25",
  32.     "Finger",
  33.     "Ping",
  34. };
  35.  
  36. /* Convert a character string containing a decimal session index number 
  37.  * into a pointer. If the arg is NULLCHAR, use the current default session.
  38.  * If the index is out of range or unused, return NULLSESSION.
  39.  */
  40. static struct session *
  41. sessptr(cp)
  42. char *cp;
  43. {
  44.     register struct session *sp;
  45.     unsigned int i;
  46.  
  47.     if(cp == NULLCHAR){
  48.         sp = Lastcurrent;
  49.     } else {
  50.         if((i = atoi(cp)) >= Nsessions)
  51.             return NULLSESSION;
  52.         sp = &Sessions[i];
  53.     }
  54.     if(sp == NULLSESSION || sp->type == FREE)
  55.         return NULLSESSION;
  56.  
  57.     return sp;
  58. }
  59.  
  60. /* Select and display sessions */
  61. dosession(argc,argv)
  62. int argc;
  63. char *argv[];
  64. {
  65.     struct session *sp;
  66.     struct sockaddr fsocket;
  67.     int i,k;
  68.     int r,t;
  69.     char *cp;
  70.  
  71.     if(argc > 1){
  72.         if((Lastcurrent = sessptr(argv[1])) != NULLSESSION)
  73.             go();
  74.         else
  75.             printf("Session %s not active\n",argv[1]);
  76.         return 0;
  77.     }
  78.     printf(" #  S#  Type     Rcv-Q Snd-Q State        Remote socket\n");
  79.     for(sp=Sessions; sp < &Sessions[Nsessions];sp++){
  80.         if(sp->type == FREE)
  81.             continue;
  82.  
  83.         i = SOCKSIZE;
  84.         k = getpeername(sp->s,(char *)&fsocket,&i);
  85.         r = socklen(sp->s,0);
  86.         t = socklen(sp->s,1);
  87.         cp = sockstate(sp->s);
  88.         printf("%c%-3u%-4d%-8s%6d%6d %-13s%s",
  89.          (Lastcurrent == sp)? '*':' ',
  90.          (unsigned)(sp - Sessions),
  91.          sp->s,
  92.          Sestypes[sp->type],
  93.          r,
  94.          t,
  95.          (cp != NULLCHAR) ? cp : "",
  96.          (sp->name != NULLCHAR) ? sp->name : "");
  97.         if(k == 0)
  98.             printf(" (%s)",psocket(&fsocket));
  99.  
  100.         if(sp->rfile != NULLCHAR || sp->ufile != NULLCHAR)
  101.             printf("\t");
  102.         if(sp->rfile != NULLCHAR)
  103.             printf("Record: %s ",sp->rfile);
  104.         if(sp->ufile != NULLCHAR)
  105.             printf("Upload: %s",sp->ufile);
  106.         printf("\n");
  107.     }
  108.     return 0;
  109. }
  110. /* Resume current session, and wait for it */
  111. int
  112. go()
  113. {
  114.     if(Lastcurrent == NULLSESSION || Lastcurrent->type == FREE)
  115.         return 0;
  116.     Current = Lastcurrent;
  117.     ttysetmode(Current->ttymode);
  118.     psignal(Current,0);
  119.     switch(Current->type){
  120.     case TELNET:
  121.         pwait(Current->cb.telnet->output);
  122.         break;
  123.     case FTP:
  124.         pwait(Current->cb.ftp->output);
  125.         break;
  126.     case AX25TNC:
  127.         pwait(Current->cb.ax25->output);
  128.         break;
  129.     case PING:
  130.         pwait(Current->cb.ping->proc);
  131.         break;
  132.     }
  133.     return 0;
  134. }
  135.  
  136. doclose(argc,argv)
  137. int argc;
  138. char *argv[];
  139. {
  140.     struct session *sp;
  141.  
  142.     if((sp = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  143.         printf(Badsess);
  144.         return -1;
  145.     }
  146.     shutdown(sp->s,1);
  147.     return 0;
  148. }
  149. doreset(argc,argv)
  150. int argc;
  151. char *argv[];
  152. {
  153.     register struct session *sp;
  154.  
  155.     if((sp = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  156.         printf(Badsess);
  157.         return -1;
  158.     }
  159.     /* Unwedge anyone waiting for a domain resolution, etc */
  160.     switch(sp->type){
  161.     case FTP:
  162.         alert(sp->cb.ftp->output,-1);
  163.         break;
  164.     case TELNET:
  165.         alert(sp->cb.telnet->output,-1);
  166.         break;
  167.     case FINGER:
  168.         alert(sp->cb.finger,-1);
  169.         break;
  170.     case AX25TNC:
  171.         break;
  172.     case PING:
  173.         alert(Current->cb.ping->proc,-1);
  174.         break;
  175.     }
  176.     shutdown(sp->s,2);
  177.     if(sp->type == FTP)
  178.         shutdown(sp->cb.ftp->data,2);
  179.     return 0;
  180. }
  181. int
  182. dokick(argc,argv)
  183. int argc;
  184. char *argv[];
  185. {
  186.     struct session *sp;
  187.  
  188.     if((sp = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  189.         printf(Badsess);
  190.         return -1;
  191.     }
  192.     sockkick(sp->s);
  193.     if(sp->type == FTP)
  194.         sockkick(sp->cb.ftp->data);
  195.     return 0;
  196. }
  197. struct session *
  198. newsession(name,type)
  199. char *name;
  200. int type;
  201. {
  202.     register struct session *sp;
  203.  
  204.     for(sp=Sessions;sp < &Sessions[Nsessions];sp++){
  205.         if(sp->type == FREE){
  206.             sp->type = type;
  207.             sp->s = -1;
  208.             if(name != NULLCHAR)
  209.                 sp->name = strdup(name);
  210.  
  211.             Current = sp;
  212.             return sp;
  213.         }
  214.     }
  215.     return NULLSESSION;
  216. }
  217. freesession(sp)
  218. struct session *sp;
  219. {
  220.     if(sp == NULLSESSION)
  221.         return;
  222.     if(sp->s != -1)
  223.         close_s(sp->s);
  224.     free_q(&sp->input);
  225.     if(sp->record != NULLFILE){
  226.         fclose(sp->record);
  227.         sp->record = NULLFILE;
  228.     }
  229.     if(sp->rfile != NULLCHAR){
  230.         free(sp->rfile);
  231.         sp->rfile = NULLCHAR;
  232.     }
  233.     if(sp->upload != NULLFILE){
  234.         fclose(sp->upload);
  235.         sp->upload = NULLFILE;
  236.     }
  237.     if(sp->ufile != NULLCHAR){
  238.         free(sp->ufile);
  239.         sp->ufile = NULLCHAR;
  240.     }
  241.     if(sp->name != NULLCHAR){
  242.         free(sp->name);
  243.         sp->name = NULLCHAR;
  244.     }
  245.     sp->type = FREE;
  246.     if(Current == sp)
  247.         Current = NULLSESSION;
  248. }
  249. /* Control session recording */
  250. dorecord(argc,argv)
  251. int argc;
  252. char *argv[];
  253. {
  254.     if(Lastcurrent == NULLSESSION){
  255.         printf("No current session\n");
  256.         return 1;
  257.     }
  258.     if(argc > 1){
  259.         if(Lastcurrent->rfile != NULLCHAR){
  260.             fclose(Lastcurrent->record);
  261.             free(Lastcurrent->rfile);
  262.             Lastcurrent->record = NULLFILE;
  263.             Lastcurrent->rfile = NULLCHAR;
  264.         }
  265.         /* Open new record file, unless file name is "off", which means
  266.          * disable recording
  267.          */
  268.         if(strcmp(argv[1],"off") != 0
  269.          && (Lastcurrent->record = fopen(argv[1],"a")) != NULLFILE){
  270.             Lastcurrent->rfile = strdup(argv[1]);
  271.         }
  272.     }
  273.     if(Lastcurrent->rfile != NULLCHAR)
  274.         printf("Recording into %s\n",Lastcurrent->rfile);
  275.     else
  276.         printf("Recording off\n");
  277.     return 0;
  278. }
  279. /* Control file transmission */
  280. doupload(argc,argv)
  281. int argc;
  282. char *argv[];
  283. {
  284.     struct telnet *tn;
  285.     struct ax25tnc *axp;
  286.  
  287.     if(Lastcurrent == NULLSESSION){
  288.         printf("No current session\n");
  289.         return 1;
  290.     }
  291.     if(argc < 2){
  292.         if(Lastcurrent->ufile != NULLCHAR)
  293.             printf("Uploading %s\n",Lastcurrent->ufile);
  294.         else
  295.             printf("Uploading off\n");
  296.         return 0;
  297.     }
  298.     switch(Lastcurrent->type){
  299.     case TELNET:
  300.         tn = Lastcurrent->cb.telnet;
  301.         break;
  302.     case AX25TNC:
  303.         axp = Lastcurrent->cb.ax25;
  304.         break;
  305.     default:
  306.         printf("Uploading not supported\n");
  307.         return 1;
  308.     }
  309.     if(strcmp(argv[1],"stop") == 0 && Lastcurrent->upload != NULLFILE){
  310.         /* Abort upload */
  311.         fclose(Lastcurrent->upload);
  312.         Lastcurrent->upload = NULLFILE;
  313.         if(Lastcurrent->ufile != NULLCHAR){
  314.             free(Lastcurrent->ufile);
  315.             Lastcurrent->ufile = NULLCHAR;
  316.         }
  317.         if(tn != NULLTN){
  318.             killproc(tn->upload);
  319.             tn->upload = NULLPROC;
  320.         } else if(axp != NULLTNC){
  321.             killproc(axp->upload);
  322.             axp->upload = NULLPROC;
  323.         }
  324.         return 0;
  325.     }
  326.     /* Open upload file */
  327.     if((Lastcurrent->upload = fopen(argv[1],"r")) == NULLFILE){
  328.         printf("Can't read %s\n",argv[1]);
  329.         return 1;
  330.     }
  331.     Lastcurrent->ufile = strdup(argv[1]);
  332.     /* All set, invoke the upload process */
  333.     switch(Lastcurrent->type){
  334. #ifdef    AX25
  335.     case AX25TNC:
  336.         axp->upload = newproc("upload",1024,ax_upload,0,Lastcurrent);
  337.         break;
  338. #endif
  339.     case TELNET:
  340.         tn->upload = newproc("upload",1024,tel_upload,0,Lastcurrent);
  341.         break;
  342.     }
  343.     return 0;
  344. }
  345.